package br.com.romerito.gui.logo;

 
import br.com.romerito.app.MidletApp;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author Romerito Campos
 */
public class Screen extends GameCanvas implements Runnable, CommandListener {
   
    
    private ViewWindow viewWindow;
    
    private Turtle turtle;
    
    private LayerManager layer;
    
    private boolean running = true;
    
    private String commandAction = "";
    
    private StringBuffer commandActionBuffer = new StringBuffer();
    
    private String commandArg = "";
    
    private StringBuffer commandArgBuffer = new StringBuffer();
    
    private boolean controlCmd = false;
    
    private boolean controlArg = false;
    
    private String keyNumber = "";

    private String lastKey = "";

    private Command exit;

        
    public Screen() {
        super(false);

        setFullScreenMode(true);       
       
        
        viewWindow = new ViewWindow(this);

        layer = new LayerManager();

        layer.setViewWindow(0, 0, getWidth() - 2, getHeight() - 2);
        
        try {
            createSprite();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        exit = new Command("Sair", Command.BACK, 1);
        addCommand(exit);
        setCommandListener(this);

        start();
    }

    public void createSprite() throws IOException {
        String imgsource = "/spriteframes.png";
        Image img = Image.createImage(imgsource);
        turtle = new Turtle(img, 28, 28);        
        turtle.setPosition((getWidth()/2)-14, (getHeight()/2)-34);
        turtle.setFrame(turtle.getDegree());

        layer.insert(turtle, 0);
    }

    public void start() {
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        Graphics g = getGraphics();

        while (running) {

            try {
                Thread.sleep(80);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

            getInput();                      

            renderWorld(g);

            flushGraphics();
        }
    }

        
    protected void keyPressed(int keyCode) {
        super.keyPressed(keyCode);
        
        switch (keyCode) {
            case Canvas.KEY_NUM0:
                keyNumber = "0";
                break;
            case Canvas.KEY_NUM1:
                keyNumber = "1";
                break;
            case Canvas.KEY_NUM2:
                keyNumber = "2";
                break;
            case Canvas.KEY_NUM3:
                keyNumber = "3";
                break;
            case Canvas.KEY_NUM4:
                keyNumber = "4";
                break;
            case Canvas.KEY_NUM5:
                keyNumber = "5";
                break;
            case Canvas.KEY_NUM6:
                keyNumber = "6";
                break;
            case Canvas.KEY_NUM7:
                keyNumber = "7";
                break;
            case Canvas.KEY_NUM8:
                keyNumber = "8";
                break;
            case Canvas.KEY_NUM9:
                keyNumber = "9";
                break;

        }
        
        lastKey = keyNumber;      

    }

    public void getInput() {
        int key = getKeyStates();
       
        if (!controlCmd) {

            if ((key & UP_PRESSED) != 0 && keyNumber.equals("") && !lastKey.equals("2")) {

                commandActionBuffer.append("pf ");
                controlCmd = true;

            }

            if ((key & DOWN_PRESSED) != 0 && keyNumber.equals("") && !lastKey.equals("8")) {

                commandActionBuffer.append("pt ");
                controlCmd = true;

            }

            if ((key & RIGHT_PRESSED)!=0 && keyNumber.equals("") && !lastKey.equals("6")){

                commandActionBuffer.append("pd ");
                controlCmd = true;
            }

            if ((key & LEFT_PRESSED)!=0 && keyNumber.equals("") && !lastKey.equals("4")){

                commandActionBuffer.append("pe ");
                controlCmd = true;
            }
        }

        if (controlCmd && (!controlArg || controlArg)) {
            String k = keyNumber;

            if (k != null) {
                commandArgBuffer.append(k);
                controlArg = true;
            }
        }

        if (controlCmd && controlArg && ((key & FIRE_PRESSED) != 0) && !lastKey.equals("5")) {

            execute(commandAction, commandArg);
           
            commandActionBuffer.delete(0, commandActionBuffer.length());
            commandArgBuffer.delete(0, commandArgBuffer.length());
            commandAction = "";
            commandArg = "";
            commandActionBuffer.append("");
            commandArgBuffer.append("");

           
            viewWindow.actionPrint(controlCmd, controlArg, commandAction, commandArg);

            controlArg = false;
            controlCmd = false;
        }
        
        keyNumber = "";
       
        commandAction = commandActionBuffer.toString();
        commandArg = commandArgBuffer.toString();

       
        viewWindow.actionPrint(controlCmd, controlArg, commandAction, commandArg);

        flushGraphics();
    }

    public void renderWorld(Graphics g) {

        g.setColor(0, 0, 255);
        g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
        
        g.drawImage(viewWindow.getViewWindow(), 0, 0, Graphics.TOP | Graphics.LEFT);

        
        g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
       
        layer.paint(g, 0, 0);
        flushGraphics();        
    }

    public void execute(String cmd, String arg) {
               
        if (cmd.equals("pd ") || cmd.equals("pe ")) {

            if (!arg.equals("")) {

                int degree = Integer.parseInt(arg);
                turtle.transform(cmd, degree);
                
            }
        }
        
        if (cmd.equals("pf ") || cmd.equals("pt ")){

            if(!arg.equals("")){
                int distance = Integer.parseInt(arg);
                turtle.move(distance,cmd, this.viewWindow);
                
            }

        }       
        
    }

    public void commandAction(Command cmd, Displayable dis) {
        if(cmd == exit){
            MidletApp.app.showMenu();
        }
    }
}
